package vn.student.controller;

import java.util.ArrayList;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.mylib.Assignment;
import vn.student.mylib.Class;
import vn.student.mylib.Contact;
import vn.student.mylib.ListAssignmentAdapter;
import vn.student.mylib.ListClassAdapter;
import android.app.Activity;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class ViewContactDetailController {
	Activity act = null;
	String id;
	public Contact contact = null;
	boolean isInstructor = false;
	boolean isCreateNew = false;
	public ViewContactDetailController(){
		
	}
	public ViewContactDetailController(Activity act){
		this.act = act;
		
	}
	public ViewContactDetailController(Activity act,String id, boolean isInstructor,boolean isCreateNew){
		this.act = act;
		this.id = id;
		this.isInstructor = isInstructor;
		this.isCreateNew = isCreateNew;
		if(isCreateNew){
			contact = new Contact();
			if(isInstructor)
				contact.type = "TEACHER";
			else
				contact.type = "STUDENT";
		}
		else
			contact = this.getContactDetail(id);
	}
	public void viewInstructorContactDetail(){
		TextView nameLabel = (TextView) act.findViewById(R.id.contactNameLabel);
		nameLabel.setText(contact.getName());
		TextView phoneNoLabel = (TextView) act.findViewById(R.id.phoneNoLabel);
		phoneNoLabel.setText(contact.getPhoneNo());
		TextView emailLabel = (TextView) act.findViewById(R.id.emailLabel);
		emailLabel.setText(contact.getEmail());
		TextView webLabel = (TextView) act.findViewById(R.id.webLabel);
		webLabel.setText(contact.getWebsite());
		TextView noteLabel = (TextView) act.findViewById(R.id.noteLabel);
		noteLabel.setText(contact.getNote());
		TextView addLabel = (TextView) act.findViewById(R.id.addLabel);
		addLabel.setText(contact.getAddress());
	}
	public Contact getContactDetail(String id){
		AStudentDB myDB = new AStudentDB(act);
		Contact contact = null;
        if(myDB!=null){
        	contact = myDB.getInstructorContactDetail(id);
        }
        return contact;
	}
	public long updateContact(Contact contact){
		AStudentDB myDB = new AStudentDB(act);
		if(isCreateNew)
			return myDB.addNewContact(contact);
		else{
				myDB.updateContact(contact);
				return -1;
			}
	}
	public void getClassListView(ListView listView,String id2){
		AStudentDB myDB = new AStudentDB(act);
		ScrollView tmp21 = (ScrollView) act.findViewById(R.id.scrView);
		ArrayList<Class> list = null;
        if(myDB!=null){
        	list = myDB.getClassList(id2);
        }
        if(list!=null){
        	listView.getLayoutParams().height=list.size()*105+10;
        	listView.setAdapter(new ListClassAdapter(act,list,false));
        }
	}
	public void getAssignmentListView(ListView listView,String id){
		ScrollView tmp21 = (ScrollView) act.findViewById(R.id.scrView);
		AStudentDB myDB = new AStudentDB(act);
		ArrayList<Assignment> list = null;
        if(myDB!=null){
        	list = myDB.getAssignmentList(id);
        }
        if(list!=null){
        	ListAssignmentAdapter tmp = new ListAssignmentAdapter(act,list,false);
        	listView.setAdapter(new ListAssignmentAdapter(act,list,false));
        	listView.getLayoutParams().height=list.size()*70+10;
        	TextView tmp2 = (TextView) act.findViewById(R.id.title);
   	     	tmp2.setText("Assignments");
        }
	}
}
